//
//  YJGifEmotionPageView.m
//  YJB
//
//  Created by YJB on 2017/1/21.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "YJGifEmotionPageView.h"
#import "YJGifEmotionBtn.h"
#import "YJGifEmotionModel.h"

#define btnW 65
@implementation YJGifEmotionPageView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)setEmotions:(NSArray *)emotions
{
    _emotions = emotions;
    //添加表情按钮
    for (int k = 0; k < emotions.count; k ++)
    {
        YJGifEmotionBtn *btn = [[YJGifEmotionBtn alloc]init];
        btn.emotion = self.emotions[k];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
}

/**
 *  布局表情按钮
 */
-(void)layoutSubviews
{
    [super layoutSubviews];
    //内边距
    CGFloat insetW = (self.width - btnW * 4)/5;
    CGFloat insetH = (self.height - btnW * 2)/3;
    for (int i = 0; i < self.subviews.count; i ++)
    {
        YJGifEmotionBtn *btn = self.subviews[i];
        btn.width  = btnW;
        btn.height = btnW;
        btn.x = insetW + (i % emotionPageViewMaxCol) * (btnW + insetW);
        btn.y = insetH + (i / emotionPageViewMaxCol) * (btnW + insetH);
    }
}

-(void)btnAction:(YJGifEmotionBtn *)btn
{
    YJGifEmotionModel *model = btn.emotion;
    if (model.isAddBtn)
    {
      [[NSNotificationCenter defaultCenter] postNotificationName:YJGifEmotionButtonDidSelectNotification object:nil userInfo:@{@"isAddBtn":@(YES),@"gifUrl":@""}];
        
    }else if (btn.emotion.gifUrl.length == 0)
    {
        [MBProgressHUD showError:@"暂无此gif表情"];
    }else
      [[NSNotificationCenter defaultCenter] postNotificationName:YJGifEmotionButtonDidSelectNotification object:nil userInfo:@{@"isAddBtn":@(NO),@"gifUrl":model.gifUrl}];
}


@end
